package com.yl.googleplayer_yl.fragment;

import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.yl.googleplayer_yl.R;
import com.yl.googleplayer_yl.utils.CommonUtils;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/10/30 0030.
 */

public abstract class PagerLoad extends FrameLayout {

    @BindView(R.id.page_iv)
    ImageView pageIv;
    @BindView(R.id.btn_reload)
    Button btnReload;
    private View loadingView;
    private View errorView;
    private View successView;

    enum PageState {
        STATE_LOADING,//加载中的状态
        STATE_ERROR,//加载失败的状态
        STATE_SUCCESS;//加载成功的状态
    }

    private PageState mPageState = PageState.STATE_LOADING;


    public PagerLoad(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLoadingPage();
    }


    public PagerLoad(Context context) {
        this(context, null);
    }

    public PagerLoad(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 将三种布局加载到界面
     */
    private void initLoadingPage() {
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        if (loadingView == null) {
            loadingView = View.inflate(getContext(), R.layout.page_loading, null);
        }
        addView(loadingView, layoutParams);

        if (errorView == null) {
            errorView = View.inflate(getContext(), R.layout.page_error, null);
            btnReload = (Button) errorView.findViewById(R.id.btn_reload);
            // ButterKnife.bind(errorView);
            btnReload.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPageState = PageState.STATE_LOADING;
                    showPage();
                    //再次请求数据
                    loadDataAndRefreshPage();
                }
            });
        }
        addView(errorView, layoutParams);

        if (successView == null) {
            successView = initSuccessView();
        }
        if (successView == null) {
            throw new IllegalArgumentException("The method createSuccessView() can not return null!");
        } else {
            CommonUtils.removeSelfFromParent(successView);
            addView(successView, layoutParams);
        }

        showPage();

        loadDataAndRefreshPage();

    }

    /**
     * 请求数据,然后刷新界面
     */
    protected void loadDataAndRefreshPage() {
        //在子线程请求数据
        CommonUtils.runOnThread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(1000);
                Object data = initData();
                mPageState = data == null ? PageState.STATE_ERROR : PageState.STATE_SUCCESS;
                CommonUtils.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        showPage();
                    }
                });
            }
        });
    }

    /**
     * 根据状态展示不同页面
     */
    private void showPage() {
        loadingView.setVisibility(INVISIBLE);
        errorView.setVisibility(INVISIBLE);
        successView.setVisibility(INVISIBLE);
        switch (mPageState) {
            case STATE_LOADING:
                loadingView.setVisibility(VISIBLE);
                break;
            case STATE_ERROR:
                errorView.setVisibility(VISIBLE);
                break;
            case STATE_SUCCESS:
                successView.setVisibility(VISIBLE);
                break;
        }
    }


    protected abstract Object initData();

    protected abstract View initSuccessView();


}
